package com.stx.dao.impl;

import com.stx.dao.UserDao;
import com.stx.pojo.User;
import com.stx.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDaoImpl implements UserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int addUser(User user) {
        String sql = "insert into user values(?,?,?,?)";
        int result = template.update(sql, user.getId(), user.getUsername(), user.getPassword(), user.getSex());
        return result;
    }

    @Override
    public List<User> queryUser() {
        String sql = "select * from user";
        List<User> userList = template.query(sql, new BeanPropertyRowMapper<>(User.class));
        return userList;
    }

    @Override
    public int deleteUser(int id) {
        String sql ="delete from user where id=?";
        int result = template.update(sql, id);
        return result;
    }

    @Override
    public int updateUser(User user) {
        String sql = "update user set username=?,password=?,sex=? where id=?";
        int result = template.update(sql, user.getUsername(), user.getPassword(), user.getSex(), user.getId());
        return result;
    }
}
